﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Net;
using System.Text.RegularExpressions;
using System.Data.OleDb;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //这个测试用的，可以不看
        private void button1_Click(object sender, EventArgs e)
        {
            string FileName =Application.StartupPath+"\\phone.xml";   
            
           
            XmlTextWriter objXmlTextWriter = new XmlTextWriter(FileName,Encoding.UTF8);
            objXmlTextWriter.Formatting = Formatting.Indented;
            objXmlTextWriter.Indentation = 6; 
            
            
            objXmlTextWriter.WriteStartDocument();

            objXmlTextWriter.WriteStartElement("", "stock", "");



            objXmlTextWriter.WriteStartElement("", "code", "");

            objXmlTextWriter.WriteStartElement("", "code-name", "");
            objXmlTextWriter.WriteString("大秦铁路");
            objXmlTextWriter.WriteEndElement();

            objXmlTextWriter.WriteStartElement("", "code-num", "");
            objXmlTextWriter.WriteString("sh601001");
            objXmlTextWriter.WriteEndElement();
            objXmlTextWriter.WriteEndElement();





            objXmlTextWriter.WriteStartElement("", "code", "");

            objXmlTextWriter.WriteStartElement("", "code-name", "");
            objXmlTextWriter.WriteString("大秦铁路");
            objXmlTextWriter.WriteEndElement();

            objXmlTextWriter.WriteStartElement("", "code-num", "");
            objXmlTextWriter.WriteString("sh601003");
            objXmlTextWriter.WriteEndElement();
            objXmlTextWriter.WriteEndElement();










            objXmlTextWriter.WriteEndElement();
            
            objXmlTextWriter.WriteEndDocument(); 
            objXmlTextWriter.Flush();
            objXmlTextWriter.Close();  
        }


        //这个是配置股票代码列表，可以不看
        private void button2_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\code.xml");

            XmlNode xn = doc.SelectSingleNode("stock/sh");
            XmlNodeList xnl = xn.ChildNodes;//stock节点下的所有sh节点
            int i = 0;
            string searchCode = "";
            
            //MessageBox.Show(gupiao[0]);
            string[] codeNum;
            string insertdata = "";
            string htmlCode;
            MatchCollection mas;

            foreach (XmlNode codelist in xnl)//从code节点列表中取出一个
            {
                foreach (XmlNode code in codelist)//去code节点的元素
                {
                    if (code.Name == "code-num")
                    {
                        searchCode += "sh" + code.InnerText + ",";

                    }
                }
                i++;
                if ( i==650)
                {
                    int count = searchCode.Remove(searchCode.LastIndexOf(','), 1).Split(',').Length;


                    htmlCode = GetStringByUrl("http://hq.sinajs.cn/list=" + searchCode);
                    mas = Regex.Matches(htmlCode, "\"(\\S{1,})\"");
                    codeNum = searchCode.Split(',');

                    for (int j = 0; j < mas.Count; j++)
                    {

                        string[] gupiao = mas[j].Groups[1].ToString().Split(',');

                        insertdata += "insert into gupiao(num,stock_name,latest_time,cur_prize,yes_prize,today_prize,lowest,highest) values ('" + codeNum[j] + "','" + gupiao[0] + "','" + gupiao[30] + "  " + gupiao[31] + "','" + gupiao[3] + "','" + gupiao[2] + "','" + gupiao[1] + "','" + gupiao[5] + "','" + gupiao[4] + "');";
                        //insertdata = insertdata.Remove(insertdata.LastIndexOf(','), 1);

                        
                    }

                    SetInfo(insertdata);

                    insertdata = "";
                    searchCode="";

                }
                else if (i == 1300)
                {
                    htmlCode = GetStringByUrl("http://hq.sinajs.cn/list=" + searchCode);
                    mas = Regex.Matches(htmlCode, "\"(\\S{1,})\"");
                    codeNum = searchCode.Split(',');

                    for (int j = 0; j < mas.Count; j++)
                    {

                        string[] gupiao = mas[j].Groups[1].ToString().Split(',');

                        insertdata += "insert into gupiao(num,stock_name,latest_time,cur_prize,yes_prize,today_prize,lowest,highest) values('" + codeNum[j] + "','" + gupiao[0] + "','" + gupiao[30] + "  " + gupiao[31] + "','" + gupiao[3] + "','" + gupiao[2] + "','" + gupiao[1] + "','" + gupiao[5] + "','" + gupiao[4] + "');";

                    }

                    //insertdata = insertdata.Remove(insertdata.LastIndexOf(','), 1);


                    SetInfo(insertdata);
                    insertdata = "";
                    searchCode = "";
                }
                    
            }
            htmlCode = GetStringByUrl("http://hq.sinajs.cn/list=" + searchCode);
            mas = Regex.Matches(htmlCode, "\"(\\S{1,})\"");
            codeNum = searchCode.Split(',');

            for (int j = 0; j < mas.Count; j++)
            {

                string[] gupiao = mas[j].Groups[1].ToString().Split(',');

                insertdata += "insert into gupiao(num,stock_name,latest_time,cur_prize,yes_prize,today_prize,lowest,highest) values('" + codeNum[j] + "','" + gupiao[0] + "','" + gupiao[30] + "  " + gupiao[31] + "','" + gupiao[3] + "','" + gupiao[2] + "','" + gupiao[1] + "','" + gupiao[5] + "','" + gupiao[4] + "');";

            }
            SetInfo(insertdata);
            //insertdata = insertdata.Remove(insertdata.LastIndexOf(','), 1);



            insertdata = "";
            searchCode = "";


            MessageBox.Show("finish");

        }


        //调用Webservice获取单只股票信息
        private string GetStringByUrl(string strUrl)
        {

            //与指定URL创建HTTP请求
            WebRequest wrt = WebRequest.Create(strUrl);
            //获取对应HTTP请求的响应
            WebResponse wrse = wrt.GetResponse();



            //获取响应流
            Stream strM = wrse.GetResponseStream();
            //对接响应流(以"GBK"字符集)
            StreamReader SR = new StreamReader(strM, Encoding.GetEncoding("GBK"));
            //获取响应流的全部字符串
            string strallstrm = SR.ReadToEnd();
            //关闭读取流
            SR.Close();

            //返回网页html代码
            return strallstrm;
        }




        //获取股票信息的例子代码，下面两个函数
        private void button3_Click(object sender, EventArgs e)
        {
            string htmlCode = GetStringByUrl("http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx/getStockInfoByCode?theStockCode=sz000001");



            XmlDocument innerdoc = new XmlDocument();
            innerdoc.LoadXml(htmlCode);

            XmlNodeList allNode = innerdoc.DocumentElement.ChildNodes;


            string time = allNode[2].InnerText;
           // SetInfo();
        }




        //显示股票信息
        private void SetInfo(string insertdata)
        {








            string connString = "Provider = Microsoft.JET.OLEDB.4.0;Data Source = |DataDirectory|stock.mdb";
            OleDbConnection conn = new OleDbConnection(connString);
            
            OleDbCommand comm = new OleDbCommand();
            comm.Connection=conn;
            //comm.CommandText = insertdata;// "insert into gupiao(num,stock_name,latest_time,cur_prize,yes_prize,today_prize,lowest,highest) values" + insertdata;

            //System.DateTime dt = DateTime.Parse("2013-11-28 15:05:47");
            //string time = dt.ToShortTimeString();


            //comm.CommandText = "insert into gupiao(num,stock_name,time,cur_prize,yes_prize,today_prize,lowest,highest) values('sz002056','高金食品','2013/11/28','0.00','6.69','0.00','0','0.00'),('sz002056','高金食品','2013/11/28','0.00','6.69','0.00','0','0.00')";

            try
            {
                conn.Open();
                int i = 0;
                string[] insert = insertdata.Split(';');
                for (i=0; i < insert.Length-1; i++)
                {
                    comm.CommandText = insert[i];
                    comm.ExecuteNonQuery();
                }
               
            }
            catch (Exception dataex)
            {

                MessageBox.Show(dataex.ToString());
            }
            finally
            {
                conn.Close();
            }

            

        }

        private void button4_Click(object sender, EventArgs e)
        {

            StreamReader sr=new StreamReader(Application.StartupPath+"\\gupiao_sz.txt");
            string data=sr.ReadToEnd();
            sr.Close();

            //MessageBox.Show(data);

            MatchCollection ma=Regex.Matches(data,"(\\w{1,})\\((\\w{1,})\\)");
            //MessageBox.Show(ma.Count.ToString());


            StreamReader srsh = new StreamReader(Application.StartupPath + "\\gupiao_sh.txt");
            string datash = srsh.ReadToEnd();
            sr.Close();

            //MessageBox.Show(data);

            MatchCollection mash = Regex.Matches(datash, "(\\w{1,})\\((\\w{1,})\\)");


            string FileName = Application.StartupPath + "\\code.xml";


            XmlTextWriter objXmlTextWriter = new XmlTextWriter(FileName, Encoding.UTF8);
            objXmlTextWriter.Formatting = Formatting.Indented;
            objXmlTextWriter.Indentation = 6;


            objXmlTextWriter.WriteStartDocument();

            objXmlTextWriter.WriteStartElement("", "stock", "");

            objXmlTextWriter.WriteStartElement("", "sz", "");

            for (int i = 0; i < ma.Count; i++)
            {
                objXmlTextWriter.WriteStartElement("", "code", "");

                objXmlTextWriter.WriteStartElement("", "code-name", "");
                objXmlTextWriter.WriteString(ma[i].Groups[1].ToString());
                objXmlTextWriter.WriteEndElement();

                objXmlTextWriter.WriteStartElement("", "code-num", "");
                objXmlTextWriter.WriteString(ma[i].Groups[2].ToString());
                objXmlTextWriter.WriteEndElement();

                objXmlTextWriter.WriteEndElement();








            }
            objXmlTextWriter.WriteEndElement();


            objXmlTextWriter.WriteStartElement("", "sh", "");
            for (int i = 0; i < mash.Count; i++)
            {
                objXmlTextWriter.WriteStartElement("", "code", "");

                objXmlTextWriter.WriteStartElement("", "code-name", "");
                objXmlTextWriter.WriteString(mash[i].Groups[1].ToString());
                objXmlTextWriter.WriteEndElement();

                objXmlTextWriter.WriteStartElement("", "code-num", "");
                objXmlTextWriter.WriteString(mash[i].Groups[2].ToString());
                objXmlTextWriter.WriteEndElement();

                objXmlTextWriter.WriteEndElement();








            }
            objXmlTextWriter.WriteEndElement();





            objXmlTextWriter.WriteEndElement();

            objXmlTextWriter.WriteEndDocument();
            objXmlTextWriter.Flush();
            objXmlTextWriter.Close();
            MessageBox.Show(ma.Count.ToString());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string connString = "Provider = Microsoft.JET.OLEDB.4.0;Data Source = |DataDirectory|stock.mdb";
            OleDbConnection conn = new OleDbConnection(connString);

            OleDbCommand comm = new OleDbCommand();
            comm.Connection = conn;
            comm.CommandText = "delete  from gupiao";

            try
            {
                conn.Open();
                comm.ExecuteNonQuery();
            }
            catch (Exception dataex)
            {

                MessageBox.Show(dataex.ToString());
            }
            finally
            {
                conn.Close();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Application.StartupPath + "\\code.xml");

            XmlNode xn = doc.SelectSingleNode("stock/sz");
            XmlNodeList xnl = xn.ChildNodes;

            MessageBox.Show(xnl.Count.ToString());
        }

        private void button7_Click(object sender, EventArgs e)
        {
            StockData sd = GetDataFromYahooUtil.GetStockCsvData("600629.ss", "2014/2/28");
            MessageBox.Show(sd.Code);
        }

        

    }
}
